package com.gxuwz.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxuwz.core.model.Cols;
import com.gxuwz.core.model.Page;
import com.gxuwz.core.util.MainUtil;
import com.gxuwz.core.util.UidUtils;
import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.DataEntity;
import com.gxuwz.pojo.Form;
import com.gxuwz.pojo.SysUser;
import com.gxuwz.pojo.Topic;
import com.gxuwz.service.IDataService;
import com.gxuwz.service.IFormService;
import com.gxuwz.service.ITopicService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 数据获取处理
 * @author Administrator
 *
 */
public class DataAction extends BaseAction implements Preparable, ModelDriven<Form>{

	protected static final String FORM_LIST_JSP = "/WEB-INF/page/data/data_form_list.jsp"; 
	protected static final String LIST_JSP = "/WEB-INF/page/data/data_list.jsp"; 
	protected static final String CHART_JSP = "/WEB-INF/page/data/data_chart.jsp"; 
	
	

	@Autowired
	private IFormService formService;
	@Autowired
	private ITopicService topicService;
	@Autowired
	private IDataService dataService;
	
	private Page page;
	private DataEntity dataEntity;
	private Form form = new Form();
	
	private List<DataEntity> dataList;


	private String json;
	private String search;
	private String dataJson;
	
	
	public String toFormList(){
		setForwardView(FORM_LIST_JSP);
		return SUCCESS;
	}
	
	/**
	 * 模糊查询获得数据
	 */
	public void getList(){
		page = new Page();
		page.setCode(0);
		List<DataEntity> dataList = null;
		
		if(form != null && form.getId() != null){
			form = formService.findById(form.getId());
			List<Topic> topicList = topicService.findByProperty("formId", form.getFormId());
			dataList = dataService.find("data_"+form.getId(), dataEntity, topicList);
			page.setCount(dataList.size());
		}else{
			page.setCount(0);
		}
		
		

		page.setData(dataList);
		reJson(page);
		return;
	}
	
	public void searchData(){
		List<DataEntity> dataList = null;
		if(dataJson != null && !"".equals(dataJson)){
			Gson gson = new Gson();
			Map<String, String> map = gson.fromJson(dataJson, new TypeToken<Map<String, String>>() {}.getType());
			form.setId(Integer.valueOf(map.get("id")));
			form = formService.findById(form.getId());
			List<Topic> topicList = topicService.findByProperty("formId", form.getFormId());
			dataEntity.setDatas(MainUtil.getSearchDatas(topicList, map));
			dataEntity.setDataState(1);
			dataList = dataService.find("data_"+form.getId(), dataEntity, topicList);
		}
		
		reJson(dataList);
		
		return;
	}
	
	/**
	 * 到数据列表
	 * @return
	 */
	public String toDataList(){
		//动态表格数据
		form = formService.findById(form.getId());
		List<Topic> topicList = topicService.findByProperty("formId", form.getFormId());


		json = MainUtil.getThead(topicList);
		search = MainUtil.getSearch(topicList);

		
		if(form != null && form.getId() != null){
			form = formService.findById(form.getId());
			//显示已填写的数据
			dataEntity.setDataState(1);

			dataList = dataService.find("data_"+form.getId(), dataEntity, topicList);
		}
		setForwardView(LIST_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 保存内容信息，创建表结构
	 * @return
	 */
	public void saveTopic(){
		Gson gson = new Gson();
		List<Topic> topics = gson.fromJson(json, new TypeToken<List<Topic>>(){}.getType());
		form = formService.findByProperty("formId", form.getFormId()).get(0);
		for(Topic tc : topics){
			tc.setFormId(form.getFormId());
			topicService.save(tc);
		}
		topics = topicService.findByProperty("formId", form.getFormId());
		dataService.createDB("data_"+form.getId(), topics);
		return;
	}
	
	/**
	 * 添加填写者
	 */
	public void addWriter(){
		Gson gson = new Gson();
		List<SysUser> writers = gson.fromJson(json, new TypeToken<List<SysUser>>(){}.getType());
		form = formService.findByProperty("formId", form.getFormId()).get(0);
		dataService.addWriter("data_"+form.getId(), writers);
		return;
	}
	
	/**
	 * 获得当前表格的填写人
	 * @return
	 */
	public void getWriter(){
		List<SysUser> userList = dataService.getWriter("data_"+form.getId());
		page = new Page();
		page.setCode(0);
		page.setData(userList);
		page.setCount(page.getData().size());

		reJson(page);
	}
	

	
	public void edit(){
		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(json, Map.class);
		dataService.update("data_"+form.getId(), map);
		return;
	}
	

	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	@Override
	public Form getModel() {
		// TODO Auto-generated method stub
		return form;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}

	public DataEntity getDataEntity() {
		return dataEntity;
	}

	public void setDataEntity(DataEntity dataEntity) {
		this.dataEntity = dataEntity;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		form = new Form();
		dataEntity = new DataEntity();
	}

	public List<DataEntity> getDataList() {
		return dataList;
	}

	public void setDataList(List<DataEntity> dataList) {
		this.dataList = dataList;
	}
	
	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getDataJson() {
		return dataJson;
	}

	public void setDataJson(String dataJson) {
		this.dataJson = dataJson;
	}

	
	
	
}
